﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.QUMT_IN020040UV01_Sssq;
using YLW.Plat.Jhfw.Model.JhfwSssqd;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_QUMT_IN020040UV01_Sssq
    {
        public static string OperationAppInfoQuery_Response(GetJhfwSssqdListOut jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new QUMT_IN020040UV01
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = Guid.NewGuid().ToString() },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "QUMT_IN020040UV01" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = receiver_name
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = sender_name
                        }
                    }
                },
                acknowledgement = new acknowledgement
                {
                    typeCode = jsonData.subcode == "success" ? "AA" : "AE",
                    targetMessage = new targetMessage
                    {
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.1",
                            extension = id_extension
                        }
                    },
                    acknowledgementDetail = new acknowledgementDetail
                    {
                        text = new text
                        {
                            value = jsonData.submsg
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",

                    queryAck = new queryAck
                    {
                        queryResponseCode = new queryResponseCode { code = jsonData.subcode == "success" ? "OK" : "error" },
                    }
                },
            };

            List<subject> subList = new List<subject>();

            if (jsonData != null && jsonData.data != null)
            {
                foreach (var listItem in jsonData.data)
                {
                    //性别
                    string sexCode = "";
                    string sexName = "";
                    switch (listItem.psex)
                    {
                        case "B":
                            sexCode = "9";
                            sexName = "未说明的性别";
                            break;
                        case "F":
                            sexCode = "2";
                            sexName = "女性";
                            break;
                        case "M":
                            sexCode = "1";
                            sexName = "男性";
                            break;
                        case "U":
                            sexCode = "0";
                            sexName = "未知的性别";
                            break;
                    }

                    subList.Add(new subject
                    {
                        typeCode = "SUBJ",
                        procedureRequest = new procedureRequest
                        {
                            classCode = "PROC",
                            moodCode = "RQO",
                            Items = new object[]
                            {
                                new id
                                {
                                    item= new item[]
                                    {
                                        new item{
                                            root="2.16.156.10011.1.24",
                                            extension=listItem.sqdh
                                        }
                                    }
                                },
                                new code(),
                                new text{
                                    value = listItem.sqdmc
                                },
                                new statusCode(),
                                new effectiveTime
                                {
                                    low=new low{ value=listItem.kdrq }
                                },
                                new methodCode
                                {
                                    item=new item[]
                                    {
                                        new item
                                        {
                                            code=listItem.mzfsdm,
                                            codeSystem="2.16.156.10011.2.3.1.159",
                                            Item=new displayName{ value=listItem.mzfsmc }
                                        },
                                        new item
                                        {
                                            code=listItem.ssxzdm,
                                            codeSystem="2.16.156.10011.2.5.1.15",
                                            Item=new displayName{ value=listItem.ssxzmc }
                                        }
                                    }
                                },
                                new author
                                {
                                    typeCode="AUT",
                                    contextControlCode="OP",
                                    assignedEntity=new assignedEntity
                                    {
                                        classCode="ASSIGNED",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item
                                                {
                                                    root="2.16.156.10011.1.4",
                                                    extension=listItem.kdysdm
                                                }
                                            }
                                        },
                                        assignedPerson=new assignedPerson
                                        {
                                            classCode="PSN",
                                            determinerCode="INSTANCE",
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    Item=new part{ value=listItem.kdysxm }
                                                }
                                            }
                                        },
                                        representedOrganization=new representedOrganization
                                        {
                                            classCode="ORG",
                                            determinerCode="INSTANCE",
                                            id=new id
                                            {
                                                item=new item[]
                                                {
                                                    new item
                                                    {
                                                        root="2.16.156.10011.2.3.2.62",
                                                        extension=listItem.kdksdm
                                                    }
                                                }
                                            },
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    Item=new part{ value=listItem.kdksmc }
                                                }
                                            }
                                        }
                                    }
                                },
                                new verifier
                                {
                                    typeCode="VRF",
                                    time=new time
                                    {
                                        value=listItem.shrq
                                    },
                                    assignedEntity=new assignedEntity
                                    {
                                        classCode="ASSIGNED",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item
                                                {
                                                    root="2.16.156.10011.1.4",
                                                    extension=listItem.shrdm
                                                }
                                            }
                                        },
                                        assignedPerson=new assignedPerson
                                        {
                                            classCode="PSN",
                                            determinerCode="INSTANCE",
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    Item=new part{ value=listItem.shrxm }
                                                }
                                            }
                                        }
                                    }
                                },
                                new component2{
                                    procedureRequest=new procedureRequest
                                    {
                                        classCode="PROC",
                                        moodCode="RQO",
                                        Items=new object[]
                                        {
                                            new id(),
                                            new code{
                                                code1=Convert.ToDecimal(listItem.ssbm),
                                                codeSystem="2.16.156.10011.2.3.3.12",
                                                displayName=new displayName{ value = listItem.ssmc }
                                            },
                                            new statusCode(),
                                            new priorityCode
                                            {
                                                code=listItem.ssdjdm,
                                                codeSystem="2.16.156.10011.2.3.1.258",
                                                displayName=new displayName { value = listItem.ssdjmc }
                                            },
                                            new performer
                                            {
                                                typeCode="PRF",
                                                time=new time
                                                {
                                                    low=new low{ value=listItem.ydsssj }
                                                },
                                                assignedEntity=new assignedEntity
                                                {
                                                    classCode="ASSIGNED",
                                                    id=new id
                                                    {
                                                        item=new item[]
                                                        {
                                                            new item{ root="", extension="" }
                                                        }
                                                    },
                                                    code=new code
                                                    {
                                                        displayName=new displayName{ value=listItem.ydsssj }
                                                    },
                                                    assignedPerson=new assignedPerson
                                                    {
                                                        classCode="",
                                                        determinerCode="",
                                                        name=new name
                                                        {
                                                            item=new item
                                                            {
                                                                Item=new part{ value="" }
                                                            }
                                                        }
                                                    },
                                                    representedOrganization=new representedOrganization
                                                    {
                                                        classCode="",
                                                        determinerCode="",
                                                        id=new id
                                                        {
                                                            item=new item[]
                                                            {
                                                                new item{ root="", extension="" }
                                                            }
                                                        },
                                                        name=new name
                                                        {
                                                            item=new item
                                                            {
                                                                Item=new part{ value="" }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                new subjectOf6
                                {
                                    contextConductionInd=true,
                                    seperatableInd=new seperatableInd { value=false },
                                    annotation=new annotation
                                    {
                                        text=new text
                                        {
                                            value=listItem.remark
                                        },
                                        statusCode=new statusCode(),
                                        author=new author
                                        {
                                            typeCode="AUT",
                                            assignedEntity=new assignedEntity
                                            {
                                                classCode="ASSIGNED"
                                            }
                                        }
                                    }
                                },
                                new componentOf1
                                {
                                    contextConductionInd=false,
                                    typeCode="COMP",
                                    contextControlCode="OP",
                                    encounter=new encounter
                                    {
                                        classCode="ENC",
                                        moodCode="EVN",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item
                                                {
                                                    root="2.16.156.10011.2.5.1.8",
                                                    extension=listItem.series
                                                },
                                                new item
                                                {
                                                    root="2.16.156.10011.2.5.1.9",
                                                    extension=listItem.jzlsh
                                                }
                                            }
                                        },
                                        code=new code
                                        {
                                            code1=0,
                                            codeSystem="2.16.156.10011.2.3.1.271",
                                            displayName=new displayName
                                            {
                                                value=""
                                            }
                                        },
                                        statusCode=new statusCode { code="active" },
                                        effectiveTime=new effectiveTime
                                        {
                                            low=new low{ value=listItem.zyrq }
                                        },
                                        subject=new subject
                                        {
                                            typeCode="SBJ",
                                            patient=new patient
                                            {
                                                classCode="PAT",
                                                id=new id
                                                {
                                                    item=new item[]
                                                    {
                                                        new item
                                                        {
                                                            root="2.16.156.10011.2.5.1.5",
                                                            extension=""
                                                        },
                                                        new item
                                                        {
                                                            root="2.16.156.10011.2.5.1.4",
                                                            extension=listItem.mrn
                                                        },
                                                        new item
                                                        {
                                                            root="2.16.156.10011.1.11",
                                                            extension=""
                                                        },
                                                        new item
                                                        {
                                                            root="2.16.156.10011.1.12",
                                                            extension=""
                                                        }
                                                    }
                                                },
                                                patientPerson=new patientPerson
                                                {
                                                    classCode="PSN",
                                                    determinerCode="INSTANCE",
                                                    id=new id
                                                    {
                                                        item=new item[]
                                                        {
                                                            new item
                                                            {
                                                                root="2.16.156.10011.1.3",
                                                                extension=listItem.pcid
                                                            },
                                                            new item
                                                            {
                                                                root="2.16.156.10011.1.15",
                                                                extension=listItem.ybkh
                                                            }
                                                        }
                                                    },
                                                    name=new name
                                                    {
                                                        item=new item
                                                        {
                                                            Item=new part{ value=listItem.pname }
                                                        }
                                                    },
                                                    telecom=new telecom
                                                    {
                                                        item=new item
                                                        {
                                                            value=listItem.pphone
                                                        }
                                                    },
                                                    administrativeGenderCode=new administrativeGenderCode
                                                    {
                                                        code=sexCode,
                                                        codeSystem="2.16.156.10011.2.3.3.4",
                                                        displayName=new displayName { value=sexName }
                                                    },
                                                    birthTime=new birthTime
                                                    {
                                                        value=listItem.pbirthday,
                                                        originalText=new originalText
                                                        {
                                                            value=""
                                                        }
                                                    },
                                                    addr=new addr
                                                    {
                                                        item=new item
                                                        {
                                                            use="H",
                                                            Item=new part
                                                            {
                                                                type="AL",
                                                                value=listItem.paddress
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        location=new location
                                        {
                                            typeCode="LOC",
                                            time=new time(),
                                            serviceDeliveryLocation=new serviceDeliveryLocation
                                            {
                                                classCode="SDLOC",
                                                location=new location
                                                {
                                                    classCode="PLC",
                                                    determinerCode="INSTANCE",
                                                    Items=new object[]
                                                    {
                                                        new id
                                                        {
                                                            item=new item[]
                                                            {
                                                                new item
                                                                {
                                                                    extension=listItem.bedid
                                                                }
                                                            }
                                                        },
                                                        new name
                                                        {
                                                            item=new item
                                                            {
                                                                use="IDE",
                                                                Item=new part{ value=listItem.bedid }
                                                            }
                                                        },
                                                        new asLocatedEntityPartOf
                                                        {
                                                            classCode="LOCE",
                                                            location=new location
                                                            {
                                                                classCode="PLC",
                                                                determinerCode="INSTANCE",
                                                                Items=new object[]
                                                                {
                                                                    new id{
                                                                        item=new item[]{
                                                                            new item
                                                                            {
                                                                                extension=listItem.bfh
                                                                            }
                                                                        }
                                                                    },
                                                                    new name
                                                                    {
                                                                        item=new item
                                                                        {
                                                                            use="IDE",
                                                                            Item=new part{ value=listItem.bfh }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                },
                                                serviceProviderOrganization=new serviceProviderOrganization
                                                {
                                                    classCode="ORG",
                                                    determinerCode="INSTANCE",
                                                    id=new id
                                                    {
                                                        item=new item[]
                                                        {
                                                            new item{ extension=listItem.zyksdm }
                                                        }
                                                    },
                                                    name=new name
                                                    {
                                                        item=new item
                                                        {
                                                            use="IDE",
                                                            Item=new part{ value=listItem.zyksmc }
                                                        }
                                                    },
                                                    asOrganizationPartOf=new asOrganizationPartOf
                                                    {
                                                        classCode="PART",
                                                        wholeOrganization=new wholeOrganization
                                                        {
                                                            classCode="ORG",
                                                            determinerCode="INSTANCE",
                                                            id=new id
                                                            {
                                                                item=new item[]
                                                                {
                                                                    new item{ extension=listItem.zybqdm }
                                                                }
                                                            },
                                                            name=new name
                                                            {
                                                                item=new item
                                                                {
                                                                    use="IDE",
                                                                    Item=new part{ value=listItem.zybqmc }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        pertinentInformation1=new pertinentInformation1
                                        {
                                            typeCode="PERT",
                                            observationDx=new observationDx
                                            {
                                                classCode="OBS",
                                                moodCode="EVN",
                                                code=new code
                                                {
                                                    code1=Convert.ToDecimal(listItem.zdlbdm),
                                                    codeSystem="2.16.156.10011.2.5.1.10",
                                                    displayName=new displayName{ value=listItem.zdlbmc }
                                                },
                                                statusCode=new statusCode{ code="active" },
                                                value=new value
                                                {
                                                    code=listItem.jbdm,
                                                    codeSystem="2.16.156.10011.2.3.3.11",
                                                    displayName=new displayName[]
                                                    {
                                                        new displayName{ value=listItem.jbmc }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                new extension
                                {
                                    sjly = new sjly { value = new value { Text = new string[] { listItem.sjly } } },
                                    lyxt = new lyxt { value = new value { Text = new string[] { listItem.lyxt } } },
                                    hoscode = new hoscode { value = new value { Text = new string[] { listItem.hoscode } } },
                                    sqdh = new sqdh { value = new value { Text = new string[] { listItem.sqdh } } },
                                    yzid = new yzid { value = new value { Text = new string[] { listItem.yzid } } },
                                    jzlsh = new jzlsh { value = new value { Text = new string[] { listItem.jzlsh } } },
                                    mrn = new mrn { value = new value { Text = new string[] { listItem.mrn } } },
                                    pname = new pname { value = new value { Text = new string[] { listItem.pname } } },
                                    pcid = new pcid { value = new value { Text = new string[] { listItem.pcid } } },
                                    pbirthday = new pbirthday { value = new value { Text = new string[] { listItem.pbirthday } } },
                                    psex = new psex { value = new value { Text = new string[] { listItem.psex } } },
                                    pphone = new pphone { value = new value { Text = new string[] { listItem.pphone } } },
                                    page = new page { value = new value { Text = new string[] { listItem.page.ToString() } } },
                                    pageunit = new pageunit { value = new value { Text = new string[] { listItem.pageunit.ToString() } } },
                                    paddress = new paddress { value = new value { Text = new string[] { listItem.paddress } } },
                                    ybkh = new ybkh { value = new value { Text = new string[] { listItem.ybkh } } },
                                    series = new series { value = new value { Text = new string[] { listItem.series } } },
                                    stay = new stay { value = new value { Text = new string[] { listItem.stay } } },
                                    zyksdm = new zyksdm { value = new value { Text = new string[] { listItem.zyksdm } } },
                                    zyksmc = new zyksmc { value = new value { Text = new string[] { listItem.zyksmc } } },
                                    zybqdm = new zybqdm { value = new value { Text = new string[] { listItem.zybqdm } } },
                                    zybqmc = new zybqmc { value = new value { Text = new string[] { listItem.zybqmc } } },
                                    bedid = new bedid { value = new value { Text = new string[] { listItem.bedid } } },
                                    bfh = new bfh { value = new value { Text = new string[] { listItem.bfh } } },
                                    zyrq = new zyrq { value = new value { Text = new string[] { listItem.zyrq } } },
                                    ssbm = new ssbm { value = new value { Text = new string[] { listItem.ssbm } } },
                                    ssmc = new ssmc { value = new value { Text = new string[] { listItem.ssmc } } },
                                    ssdjdm = new ssdjdm { value = new value { Text = new string[] { listItem.ssdjdm } } },
                                    ssdjmc = new ssdjmc { value = new value { Text = new string[] { listItem.ssdjmc } } },
                                    ssxzdm = new ssxzdm { value = new value { Text = new string[] { listItem.ssxzdm } } },
                                    ssxzmc = new ssxzmc { value = new value { Text = new string[] { listItem.ssxzmc } } },
                                    mzfsdm = new mzfsdm { value = new value { Text = new string[] { listItem.mzfsdm } } },
                                    mzfsmc = new mzfsmc { value = new value { Text = new string[] { listItem.mzfsmc } } },
                                    ssysdm = new ssysdm { value = new value { Text = new string[] { listItem.ssysdm } } },
                                    ssysxm = new ssysxm { value = new value { Text = new string[] { listItem.ssysxm } } },
                                    ydsssj = new ydsssj { value = new value { Text = new string[] { listItem.ydsssj } } },
                                    remark = new remark { value = new value { Text = new string[] { listItem.remark } } },
                                    kdksdm = new kdksdm { value = new value { Text = new string[] { listItem.kdksdm } } },
                                    kdksmc = new kdksmc { value = new value { Text = new string[] { listItem.kdksmc } } },
                                    kdysdm = new kdysdm { value = new value { Text = new string[] { listItem.kdysdm } } },
                                    kdysxm = new kdysxm { value = new value { Text = new string[] { listItem.kdysxm } } },
                                    kdrq = new kdrq { value = new value { Text = new string[] { listItem.kdrq } } },
                                    jbdm = new jbdm { value = new value { Text = new string[] { listItem.jbdm } } },
                                    jbmc = new jbmc { value = new value { Text = new string[] { listItem.jbmc } } },
                                    zdlbdm = new zdlbdm { value = new value { Text = new string[] { listItem.zdlbdm } } },
                                    zdlbmc = new zdlbmc { value = new value { Text = new string[] { listItem.zdlbmc } } },
                                    zdrq = new zdrq { value = new value { Text = new string[] { listItem.zdrq } } },
                                    shrdm = new shrdm { value = new value { Text = new string[] { listItem.shrdm } } },
                                    shrxm = new shrxm { value = new value { Text = new string[] { listItem.shrxm } } },
                                    shrq = new shrq { value = new value { Text = new string[] { listItem.shrq } } },
                                    zxksdm = new zxksdm { value = new value { Text = new string[] { listItem.zxksdm } } },
                                    zxksmc = new zxksmc { value = new value { Text = new string[] { listItem.zxksmc } } },
                                    zxsj = new zxsj { value = new value { Text = new string[] { listItem.zxsj } } },
                                    sqdmc = new sqdmc { value = new value { Text = new string[] { listItem.sqdmc } } },
                                    sqdzt = new sqdzt { value = new value { Text = new string[] { listItem.sqdzt } } },
                                    useflag = new useflag { value = new value { Text = new string[] { listItem.useflag } } },
                                    in_empid = new in_empid { value = new value { Text = new string[] { listItem.in_empid } } },
                                    in_date = new in_date { value = new value { Text = new string[] { listItem.in_date } } },
                                    xg_empid = new xg_empid { value = new value { Text = new string[] { listItem.xg_empid } } },
                                    xgsj = new xgsj { value = new value { Text = new string[] { listItem.xgsj } } }
                                }
                            }
                        }
                    });
                }
            }

            xmlRoot.controlActProcess.subject = subList.ToArray();

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
